home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 9 / develop 9 code / NeoTextBox / NTBDemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-06  |  11.9 KB  |  387 lines  |  [TEXT/MPS ]

  1. /*****************************************************************************************
  2.  
  3. NTBDemo.c - demo/test program for NeoTextBox
  4.  
  5. Written by Bryan K. Ressler (Beaker), 8/30/91
  6.  
  7. *****************************************************************************************/
  8.  
  9. #define BEAKER
  10.  
  11. /** INCLUDES ****************************************************************************/
  12. #include "NTBDemo.h"        /* NTBDemo stuff */
  13. #include "Utilities.h"        /* Utility stuff */
  14. #include "NeoTextBox.h"        /* NeoTextBox module */
  15.  
  16. /** GLOBALS *****************************************************************************/
  17. SysEnvRec    gEnvironment;    /* Our operating environment */
  18. Boolean        gHasTrueType;    /* Is TrueType around? */
  19. short        gJustRadio;        /* Currently-set justification radio button */
  20. short        gFontRadio;        /* Currently-set font radio button */
  21.  
  22. /** STATICS *****************************************************************************/
  23. char        gsBlankStr[1];    /* Blank string */
  24. short        gsTextRadio;    /* Currently-set text radio button */
  25. Boolean        gsVariable;        /* State of "Variable line height" checkbox */
  26. short        gsNumLines;        /* The number of line in the wrapped text total */
  27. short        gsEndY;            /* The endingY from NeoTextBox item */
  28. short        gsLHUsed;        /* The line height used based on the given text spec */
  29. Str255        gsInfoStr;        /* The string displayed in the info item */
  30.  
  31. /** PUBLIC PROTOTYPES *******************************************************************/
  32. void main(void);
  33.  
  34. /** STATIC PROTOTYPES *******************************************************************/
  35. static pascal void NTBItem(WindowPtr theWindow,short itemNum);
  36. static pascal void NTBInfoItem(WindowPtr theWindow,short itemNum);
  37. static void NTBPerformance(DialogPtr dialog);
  38. static void UpdateParms(DialogPtr testDialog);
  39. static void RedrawInfo(DialogPtr dialog);
  40. static void SetupDialog(DialogPtr testDialog);
  41. static void PoseDialog(DialogPtr testDialog);
  42. static void DemoNTB(void);
  43. static Boolean Initialize(void);
  44.  
  45. /****************************************************************************************/
  46. pascal void NTBItem(WindowPtr theWindow,short itemNum)
  47. {
  48.     short    aType;
  49.     Rect    theBox;
  50.     Handle    aHandle,text;
  51.     long    textLen;
  52.     short    just,lineHeight;
  53.     Boolean    oldPreserve,oldPreferred;
  54.     short    oldLH,oldNL,oldEY;
  55.  
  56.     GetDItem(theWindow,itemNum,&aType,&aHandle,&theBox);
  57.     PenNormal(); PenPat(&qd.gray);
  58.     ForeColor(blackColor);
  59.     FrameRect(&theBox);
  60.     PenNormal();
  61.     
  62.     InsetRect(&theBox,4,4);
  63.     EraseRect(&theBox);
  64.     
  65.     text = GetResource('TEXT',(gsTextRadio == kShannonText) ? kShannonID : kBarkerID);
  66.     if (text) {
  67.         oldLH = gsLHUsed;                    /* Remember these so we can check for diffs */
  68.         oldNL = gsNumLines;
  69.         oldEY = gsEndY;
  70.         
  71.         just = RadioToJust();                /* Find out what justification to use */
  72.         textLen = GetHandleSize(text);        /* Determine text length */
  73.         HLock(text);                        /* Lock text - NeoTextBox may move memory */
  74.         
  75.         TextParms(kSave);                    /* Save current port text parameters */
  76.         
  77.         TextFont(RadioToFont());            /* Set up the port's text settings */
  78.         TextFace(0);
  79.         TextSize(GetDefFontSize());
  80.         TextMode(srcOr);
  81.         
  82.         if (gHasTrueType) {
  83.             oldPreferred = GetOutlinePreferred();    /* Save old, prefer TrueType */
  84.             SetOutlinePreferred(true);
  85.             
  86.             if (gsVariable) {
  87.                 oldPreserve = GetPreserveGlyph();    /* Save old, preserve glyphs */
  88.                 SetPreserveGlyph(true);
  89.             }
  90.         }
  91.         
  92.         lineHeight = (gsVariable) ? -1 : 0;
  93.         
  94.         /* This is the fancy-schmancy way, see InfoItem for a simple example */
  95.         gsNumLines = NeoTextBox(*text,textLen,&theBox,just,lineHeight,&gsEndY,&gsLHUsed);
  96.         
  97.         if (gHasTrueType) {                    /* Restore TrueType settings */
  98.             SetOutlinePreferred(oldPreferred);
  99.             if (gsVariable)
  100.                 SetPreserveGlyph(oldPreserve);
  101.         }
  102.         
  103.         TextParms(kRestore);                /* Restore the port's old text parameters */
  104.         
  105.         if (gsEndY <= theBox.bottom) {
  106.             PenPat(&qd.ltGray);                /* Draw a light gray line below the text */
  107.             PenMode(patOr);
  108.             ForeColor(blueColor);
  109.             MoveTo(theBox.left,gsEndY);
  110.             LineTo(theBox.right - 1,gsEndY);
  111.             ForeColor(blackColor);
  112.             PenNormal();
  113.         }
  114.         
  115.         if (gsLHUsed != oldLH || gsNumLines != oldNL || gsEndY != oldEY)
  116.             UpdateParms(theWindow);            /* Redraw the stats if they've changed */
  117.     }
  118. }
  119.  
  120. /****************************************************************************************/
  121. pascal void NTBInfoItem(WindowPtr theWindow,short itemNum)
  122. {
  123.     short    aType;
  124.     Rect    theBox;
  125.     Handle    aHandle;
  126.  
  127.     GetDItem(theWindow,itemNum,&aType,&aHandle,&theBox);
  128.     ForeColor(blackColor);
  129.     
  130.     EraseRect(&theBox);
  131.     
  132.     TextParms(kSave);                    /* Save current port text parameters */
  133.         
  134.     TextFont(GetAppFont());            /* Set up the port's text settings */
  135.     TextFace(0);
  136.     TextSize(9);
  137.     TextMode(srcOr);
  138.     
  139.     /* This is the simple, TextBox-like way to call NeoTextBox */
  140.     NeoTextBox(gsInfoStr + 1,*gsInfoStr,&theBox,GetSysJust(),0,nil,nil);
  141.     
  142.     TextParms(kRestore);                /* Restore the port's old text parameters */
  143. }
  144.  
  145. /****************************************************************************************/
  146. void NTBPerformance(DialogPtr dialog)
  147. {
  148.     char    buf1[40],buf2[40],buf3[40],buf4[40];
  149.     Handle    testTextHdl;
  150.     char    *testText;
  151.     long    testLen;
  152.     Rect    box;
  153.     short    i,just,percentFaster;
  154.     long    startTime,endTime,textBoxTime,neoTextBoxTime;
  155.     Fixed    factor;
  156.     Boolean    goForIt = true;
  157.     
  158.     just = RadioToJust();                        /* Set up justification */
  159.     testTextHdl = GetResource('TEXT',kTestTextID);
  160.     
  161.     if (just == ntbJustFull) {
  162.         goForIt = false;
  163.         StopAlert(kNoCanDoAlrtID,nil);
  164.     }
  165.     
  166.     if (goForIt) {
  167.         NumToString(kTestIterations,(Str255)buf1);    /* Put stats dialog up */
  168.         ParamText(buf1,gsBlankStr,gsBlankStr,gsBlankStr);
  169.         goForIt = (NoteAlert(kDoPerfAlrtID,nil) == kOkayButton);
  170.     }
  171.     
  172.     if (goForIt && testTextHdl) {
  173.         testLen = GetHandleSize(testTextHdl);
  174.         HLock(testTextHdl);
  175.         testText = *testTextHdl;
  176.         
  177.         GetItemRect(dialog,kTextArea,&box);
  178.         InsetRect(&box,4,4);
  179.         
  180.         GetIndString(gsInfoStr,kStringsID,kCachingPass);
  181.         RedrawInfo(dialog);
  182.         TextParms(kSave);                            /* Save text parameters */
  183.         TextFont(RadioToFont());                    /* Set up the port's text settings */
  184.         TextFace(0);
  185.         TextSize(GetDefFontSize());
  186.         TextMode(srcOr);
  187.         TextBox(testText,testLen,&box,just);
  188.         EraseRect(&box);
  189.         NeoTextBox(testText,testLen,&box,just,0,nil,nil);
  190.         TextParms(kRestore);                        /* Restore port's text settings */
  191.         
  192.         GetIndString(gsInfoStr,kStringsID,kTestingTextBox);
  193.         RedrawInfo(dialog);
  194.         TextParms(kSave);                            /* Save text parameters */
  195.         TextFont(RadioToFont());                    /* Set up the port's text settings */
  196.         TextFace(0);
  197.         TextSize(GetDefFontSize());
  198.         TextMode(srcOr);
  199.         startTime = TickCount();
  200.         for (i = 0; i < kTestIterations; i++)
  201.             TextBox(testText,testLen,&box,just);
  202.         endTime = TickCount();
  203.         textBoxTime = endTime - startTime;
  204.         TextParms(kRestore);                        /* Restore port's text settings */
  205.         
  206.         GetIndString(gsInfoStr,kStringsID,kTestingNeoTextBox);
  207.         RedrawInfo(dialog);
  208.         TextParms(kSave);                            /* Save text parameters */
  209.         TextFont(RadioToFont());                    /* Set up the port's text settings */
  210.         TextFace(0);
  211.         TextSize(GetDefFontSize());
  212.         TextMode(srcOr);
  213.         startTime = TickCount();
  214.         for (i = 0; i < kTestIterations; i++) {
  215.             EraseRect(&box);
  216.             NeoTextBox(testText,testLen,&box,just,0,nil,nil);
  217.         }
  218.         endTime = TickCount();
  219.         neoTextBoxTime = endTime - startTime;
  220.         TextParms(kRestore);                        /* Restore port's text settings */
  221.         
  222.         HUnlock(testTextHdl);
  223.             
  224.         NumToString(kTestIterations,(Str255)buf1);    /* Put stats dialog up */
  225.         NumToString(textBoxTime,(Str255)buf2);
  226.         NumToString(neoTextBoxTime,(Str255)buf3);
  227.         factor = FixRatio(textBoxTime,neoTextBoxTime) - Long2Fix(1);
  228.         percentFaster = FixRound(FixMul(factor,Long2Fix(100)));
  229.         NumToString(percentFaster,buf4);
  230.         ParamText(buf1,buf2,buf3,buf4);
  231.         NoteAlert(kPerfAlrtID,nil);
  232.                 
  233.         GetIndString(gsInfoStr,kStringsID,kStdInfo);    /* Restore info item text */
  234.         InvalItem(dialog,kInfoItem,0,0);
  235.         InvalItem(dialog,kTextArea,4,4);
  236.     }
  237.  
  238.     UpdateParms(dialog);
  239. }
  240.  
  241. /****************************************************************************************/
  242. void UpdateParms(DialogPtr dialog)
  243. {
  244.     char    convBuffer1[40],convBuffer2[40],convBuffer3[40];
  245.     
  246.     NumToString(gsEndY,(Str255)convBuffer1);
  247.     NumToString(gsLHUsed,(Str255)convBuffer2);
  248.     NumToString(gsNumLines,(Str255)convBuffer3);
  249.     ParamText(convBuffer1,convBuffer2,convBuffer3,gsBlankStr);
  250.     
  251.     InvalItem(dialog,kValuesItem,0,0);
  252. }
  253.  
  254. /****************************************************************************************/
  255. void RedrawInfo(DialogPtr dialog)
  256. {
  257.     InvalItem(dialog,kInfoItem,0,0);
  258.     BeginUpdate(dialog);
  259.     UpdtDialog(dialog,dialog->visRgn);
  260.     EndUpdate(dialog);
  261. }
  262.  
  263. /****************************************************************************************/
  264. void SetupDialog(DialogPtr testDialog)
  265. {
  266.     short    i;
  267.     
  268.     gJustRadio = JustToRadio(GetSysJust());            /* Default to system justification */
  269.     gsVariable = false;                                /* Default to default line height */
  270.     gFontRadio = kAppFont;                            /* Default to application font */
  271.     gsTextRadio = kShannonText;                        /* Default to Shannon text */
  272.     
  273.     GetIndString(gsInfoStr,kStringsID,kStdInfo);    /* Set up info item */
  274.  
  275.     SetValue(testDialog,gJustRadio,1);                /* Set initial values */
  276.     SetValue(testDialog,kVariableHeight,gsVariable ? 1 : 0);
  277.     SetValue(testDialog,gFontRadio,1);
  278.     SetValue(testDialog,gsTextRadio,1);
  279.     
  280.     for (i = kBoxOne; i <= kLastBox; i++)
  281.         UserItem(testDialog,i,BoxItem);                /* Install box user items */
  282.     for (i = kDividerOne; i <= kLastDivider; i++)
  283.         UserItem(testDialog,i,GrayBoxItem);            /* Install divider user items */
  284.     UserItem(testDialog,kTextArea,NTBItem);            /* Install the main text item */
  285.     UserItem(testDialog,kInfoItem,NTBInfoItem);        /* Install the information item */
  286.     
  287.     NTBItem(testDialog,kTextArea);                    /* Draw (to set Y/line height vars) */
  288.  
  289.     ShowWindow(testDialog);                            /* Put it on the screen */
  290. }
  291.  
  292. /****************************************************************************************/
  293. void PoseDialog(DialogPtr testDialog)
  294. {
  295.     short    itemHit;
  296.  
  297.     do {
  298.         ModalDialog(nil,&itemHit);
  299.         switch (itemHit) {
  300.             case kPerfButton:
  301.                 NTBPerformance(testDialog);
  302.                 break;
  303.             case kJustLeft:                        /* Justification cluster */
  304.             case kJustCenter:
  305.             case kJustRight:
  306.             case kJustFull:
  307.                 if (gJustRadio != itemHit) {
  308.                     SetValue(testDialog,gJustRadio,0);
  309.                     gJustRadio = itemHit;
  310.                     SetValue(testDialog,gJustRadio,1);
  311.                     InvalItem(testDialog,kTextArea,4,4);
  312.                 }
  313.                 break;
  314.             case kVariableHeight:                /* Variable height checkbox */
  315.                 gsVariable = !gsVariable;
  316.                 SetValue(testDialog,kVariableHeight,gsVariable ? 1 : 0);
  317.                 InvalItem(testDialog,kTextArea,4,4);
  318.                 break;
  319.             case kAppFont:                        /* Font cluster */
  320.             case kTimes:
  321.             case kHelvetica:
  322.                 if (gFontRadio != itemHit) {
  323.                     SetValue(testDialog,gFontRadio,0);
  324.                     gFontRadio = itemHit;
  325.                     SetValue(testDialog,gFontRadio,1);
  326.                     InvalItem(testDialog,kTextArea,4,4);
  327.                 }
  328.                 break;
  329.             case kShannonText:                    /* Test cluster */
  330.             case kLiterature:
  331.                 if (gsTextRadio != itemHit) {
  332.                     SetValue(testDialog,gsTextRadio,0);
  333.                     gsTextRadio = itemHit;
  334.                     SetValue(testDialog,gsTextRadio,1);
  335.                     InvalItem(testDialog,kTextArea,4,4);
  336.                 }
  337.                 break;
  338.             default:
  339.                 break;
  340.         }
  341.     } while (itemHit != kQuitButton);
  342. }
  343.  
  344. /****************************************************************************************/
  345. void DemoNTB(void)
  346. {
  347.     GrafPtr        savePort;
  348.     DialogPtr    testDialog;
  349.     
  350.     GetPort(&savePort);
  351.     testDialog = GetNewDialog(kTestDlgID,nil,(WindowPtr)-1);
  352.     if (testDialog) {
  353.         SetPort(testDialog);
  354.         SetupDialog(testDialog);
  355.         PoseDialog(testDialog);
  356.         DisposDialog(testDialog);
  357.     }
  358.     SetPort(savePort);
  359. }
  360.  
  361. /****************************************************************************************/
  362. Boolean Initialize(void)
  363. {
  364.     OSErr    err;
  365.     
  366.     InitGraf(&qd.thePort);            /* The usual Mac inits */
  367.     InitFonts();
  368.     InitWindows();
  369.     TEInit();
  370.     InitDialogs(nil);
  371.     InitCursor();
  372.     
  373.     *gsBlankStr = 0;
  374.     gHasTrueType = (NGetTrapAddress(kTrueTypeTrap,ToolTrap) !=
  375.         NGetTrapAddress(kUnimplTrap,ToolTrap));
  376.     
  377.     err = SysEnvirons(2,&gEnvironment);
  378.     return((err || gEnvironment.systemVersion < 0x0600) ? false : true);
  379. }
  380.  
  381. /****************************************************************************************/
  382. void main(void)
  383. {
  384.     if (Initialize())
  385.         DemoNTB();
  386. }
  387.